#ifndef __THREADPOOL_H
#define __THREADPOOL_H

#include <stdint.h>

#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"

#include "ThreadList.h"


#define  THREAD_MAX_NUM  15



//任务结构体
typedef struct 
{
	void     (*function)(void *arg);            //任务函数
	void     *arg;                              //任务参数
}ThreadPoolTask_t;


typedef struct
{
	QueueHandle_t Task_Q;                       //任务队列
	uint16_t      Q_Capacity;                   //任务队列容量    
	uint16_t      Q_Number;                     //当前队列中未处理任务个数
	
	TaskHandle_t  ManageID;                     //管理线程
	ListHandle_t  WorkList_Head;                //工作线程指针头
	ListHandle_t  WorkList_End;                 //工作线程指针尾
	
	uint8_t min_num;                            //最小线程数
	uint8_t max_num;                            //最大线程数
	uint8_t busy_num;                           //忙线程数
	uint8_t alive_num;                          //存活线程数
	
	SemaphoreHandle_t  ThreadLock;              //线程锁
	SemaphoreHandle_t  BusyLock;                //繁忙线程锁
	uint8_t            shutdown;                //是否需要销毁线程(0表示不需要，1表示需要)
}ThreadPool_t;

typedef   ThreadPool_t* PoolHandle_t;

uint8_t ThreadPoolDestroy(PoolHandle_t pool);                             //销毁线程池
uint8_t GetThreadPoolAlive(PoolHandle_t pool);                            //获取存活线程数
uint8_t GetThreadPoolWorker(PoolHandle_t pool);                           //获取正在工作线程数
void AddThreadPoolTask(PoolHandle_t pool,void (*func)(void*),void *arg);  //添加任务
PoolHandle_t ThreadPoolCreate(uint8_t min,uint8_t queuesize); //创建线程池

#endif







